from odoo import api, fields, models, _
import logging
from odoo.exceptions import UserError

logger = logging.getLogger(__name__)

class InfoMER_Ma(models.Model):
    _name = 'user.search'
    _description = 'user_info'

    number_no = fields.Char(
        '患者编号',
        index=True,
        readonly=True,
        default='自动生成'
    )
    #
    # @api.model
    # def create(self, vals):
    #     """
    #     自动生成单号
    #     :return:
    #     """
    #     vals['number_no'] = self.env['ir.sequence'].next_by_code('user.info') or ''
    #     return super(InfoMER, self).create(vals)

    name = fields.Char(
        '姓名',
        required=True,
    )
    member_image = fields.Binary(
        string='头像'
    )
    vaccine_sex = fields.Selection(
        [
            ('001', '男'),
            ('002', '女')
        ],
        '性别',
        default='001',
    )
    vaccine_nation = fields.Char(
        '民族',
        required=True,
        default='汉族',
    )
    vaccine_phone = fields.Char(
        '电话',
        required=True,
    )
    vaccine_email = fields.Char(
        '邮箱地址',
    )
    vaccine_stature = fields.Float(
        '身高(cm)',
    )
    vaccine_weight = fields.Float(
        '体重(kg)',
    )
    vaccine_adverseevent = fields.Selection(
        [
            ('001', '有'),
            ('002', '无'),
            ('003', '不详')
        ],
        '既往接种疫苗是否有不良反应与事件',
        default='001',
    )
    vaccine_family = fields.Selection(
        [
            ('001', '有'),
            ('002', '无'),
            ('003', '不详')
        ],
        '家族成员既往接种疫苗是否有不良反应与事件',
        default='001',
    )
    vaccine_medical = fields.Selection(
        [
            ('001', '肝病史'),
            ('002', '肾病史'),
            ('003', '过敏史'),
            ('003', '其他')
        ],
        '存在病史',
        default='001',
    )
    vaccine_inoculate = fields.Selection(
        [
            ('001', '百白破疫苗'),
            ('002', '乙肝疫苗'),
            ('003', '卡介疫苗'),
            ('004', '脊髓灰质疫苗'),
            ('005', '麻疹疫苗'),
            ('006', '狂犬病疫苗'),
            ('007', 'HPV疫苗'),
        ],
        '曾经接种过的疫苗',
        default='001',
    )
    priority = fields.Selection(
        [('0', '低级'),
         ('1', '中级'),
         ('2', '高级')],
        '病人等级',
        default='1')
    color = fields.Integer()
    sequence = fields.Integer(string='Sequence')
    status = fields.Selection(
        [('默认', '默认'),
         ('康复', '康复'),
         ('特殊', '特殊')],
        '状态',
        default='默认')

    def define_special(self):
        self.ensure_one()
        # 检验此数据集中是否包含单条数据，否则报错
        sql_query = """SELECT number_no FROM user_special where number_no = '{number_no}'""".format(
            number_no=self.number_no)
        self.env.cr.execute(sql_query)
        result_spe = self.env.cr.dictfetchall()
        sql_query = """SELECT number_no FROM user_recover where number_no = '{number_no}'""".format(
            number_no=self.number_no)
        self.env.cr.execute(sql_query)
        result_re = self.env.cr.dictfetchall()
        if result_re:
            raise UserError(_('患者%s已在康复患者中存有记录！') % self.name)
        if result_spe:
            raise UserError(_('患者%s已在特殊患者中存有记录！') % self.name)
        # 有则报错
        else:
            # logger.info("Here is the name: %s", result)
            check_as_superuser = self.env['user.special'].sudo()
            # sudo():使用现有数据集创建一个新运行环境，得到一个基于新运行环境的数据集的拷贝
            check_as_superuser.create({
                'number_no': self.number_no,
                'name': self.name,
                'member_image': self.member_image,
                'vaccine_sex': self.vaccine_sex,
                'vaccine_nation': self.vaccine_nation,
                'vaccine_phone': self.vaccine_phone,
                'vaccine_email': self.vaccine_email,
                'vaccine_stature': self.vaccine_stature,
                'vaccine_weight': self.vaccine_weight,
                'vaccine_adverseevent': self.vaccine_adverseevent,
                'vaccine_family': self.vaccine_family,
                'vaccine_medical': self.vaccine_medical,
                'vaccine_inoculate': self.vaccine_inoculate,
                'priority': self.priority,
                'sequence': self.sequence,
            })
            # 创造新的一条数据
            self.status = '特殊'

            return {
                'type': 'ir.actions.act_window',
                'view_type': 'kanban',
                'view_mode': 'kanban,tree,form',
                'res_model': 'user.special',
                'target': 'new',
            }

    def define_recover(self):
        self.ensure_one()
        # 检验此数据集中是否包含单条数据，否则报错
        sql_query = """SELECT number_no FROM user_special where number_no = '{number_no}'""".format(
            number_no=self.number_no)
        self.env.cr.execute(sql_query)
        result_spe = self.env.cr.dictfetchall()
        sql_query = """SELECT number_no FROM user_recover where number_no = '{number_no}'""".format(
            number_no=self.number_no)
        self.env.cr.execute(sql_query)
        result_re = self.env.cr.dictfetchall()
        if result_re:
            raise UserError(_('患者%s已在康复患者中存有记录！') % self.name)
        if result_spe:
            raise UserError(_('患者%s已在特殊患者中存有记录！') % self.name)
        # 有则报错
        else:
            # logger.info("Here is the name: %s", result)
            check_as_superuser = self.env['user.recover'].sudo()
            # sudo():使用现有数据集创建一个新运行环境，得到一个基于新运行环境的数据集的拷贝
            check_as_superuser.create({
                'number_no': self.number_no,
                'name': self.name,
                'member_image': self.member_image,
                'vaccine_sex': self.vaccine_sex,
                'vaccine_nation': self.vaccine_nation,
                'vaccine_phone': self.vaccine_phone,
                'vaccine_email': self.vaccine_email,
                'vaccine_stature': self.vaccine_stature,
                'vaccine_weight': self.vaccine_weight,
                'vaccine_adverseevent': self.vaccine_adverseevent,
                'vaccine_family': self.vaccine_family,
                'vaccine_medical': self.vaccine_medical,
                'vaccine_inoculate': self.vaccine_inoculate,
                'sequence': self.sequence,
                'status': self.status,
            })
            # 创造新的一条数据
            self.status = '康复'

            return {
                'type': 'ir.actions.act_window',
                'view_type': 'kanban',
                'view_mode': 'kanban,tree,form',
                'res_model': 'user.recover',
                'target': 'new',
            }

